package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.service.findService;

import domain.Things;

import serviceIface.ServiceIface;

public class querryInfo extends HttpServlet {
	private ServiceIface serviceiface;
	/**
	 * Constructor of the object.
	 */
	public querryInfo() {
		serviceiface=new findService();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		this.doPost(request, response);
		PrintWriter out = response.getWriter();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String maintype="";
		if(request.getParameter("maintype")==null){
			maintype="";
		}else{
		 maintype=request.getParameter("maintype");
		}
		int count=serviceiface.countquerry(maintype);
		int page=0;
		if(count%15==0){
			page=count/15;
		}else{
			page=count/15+1;
		}
		int pagesize=15;
		int currentpage=1;
		if(request.getParameter("drip")==null||request.getParameter("drip").equals("")){
			currentpage=1;
		}else{
		currentpage=Integer.parseInt(request.getParameter("drip"));
		if(currentpage<1){
			currentpage=1;	
		}
		if(currentpage>=page){
			currentpage=page;
		}
	
		}
		List<Things> list=serviceiface.querryInfo(maintype,currentpage,pagesize);
		//System.out.println(list.get(0).getType());
		request.setAttribute("thinglist", list);
		request.setAttribute("maintype", maintype);
		request.setAttribute("page", page);
		request.setAttribute("currentpage", currentpage);
		request.getRequestDispatcher("/showInformation.jsp").forward(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
